package com.csy.admin.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csy.admin.entity.SysMenu;
import com.csy.admin.service.SysMenuService;
import com.csy.admin.util.Constant;
import com.csy.admin.util.Result;
import com.csy.common.exception.RRException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 类名:菜单控制类
 * Created by CSY on 2018/9/10.
 */
@RestController
@RequestMapping("/sysMenu")
public class MenuController extends AbstractController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 获取用户菜单
     * @return
     */
    @RequestMapping(value = "/getNav",method = RequestMethod.GET)
    public Result getNav(){

        List<SysMenu> menuList = sysMenuService.getUserMenuList(getUserId());

        return Result.ok().put("menuList",menuList);
    }

    /**
     * 获取所有菜单列表
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<SysMenu> list(){
        List<SysMenu> menuList = sysMenuService.list(null);
        for (SysMenu menu : menuList){
            SysMenu sysMenu = sysMenuService.getById(menu.getParentId());
            if(sysMenu != null) {
                menu.setParentName(sysMenu.getMenuName());
            }
        }
        return menuList;
    }

    /**
     * 获取菜单树(添加，修改菜单时选择上级菜单时用)
     * @return
     */
    @RequestMapping(value = "/select",method = RequestMethod.GET)
    public Result select(){
        List<SysMenu> menuList = sysMenuService.queryNotButtonList();
        SysMenu root = new SysMenu();
        root.setMenuId(0L);
        root.setParentId(-1L);
        root.setMenuName("一级菜单");
        root.setOpen(true);
        menuList.add(root);
        return Result.ok().put("menuList",menuList);
    }

    /**
     * 根据菜单id获取菜单信息
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/info/{menuId}",method = RequestMethod.GET)
    public Result info(@PathVariable("menuId") Long menuId){
        SysMenu menu = sysMenuService.getById(menuId);
        return Result.ok().put("menu",menu);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result save(@RequestBody SysMenu menu){
        verifyForm(menu);
        sysMenuService.save(menu);
        return Result.ok();
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody SysMenu menu){
        verifyForm(menu);
        sysMenuService.updateById(menu);
        return Result.ok();
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Result delete(Long menuId){
        List<SysMenu> menuList = sysMenuService.list(
                new QueryWrapper<SysMenu>().eq("parent_id",menuId));
        if(menuList.size() > 0){
            return Result.error("请先删除子菜单或按钮");
        }
        sysMenuService.removeById(menuId);
        return Result.ok();
    }

    /**
     * 验证参数是否正确
     * @param menu
     */
    private void verifyForm(SysMenu menu){
        if(StringUtils.isBlank(menu.getMenuName())){
            throw new RRException("菜单名称不能为空");
        }

        if(menu.getParentId() == null){
            throw new RRException("上级菜单不能为空");
        }
        //获取上级菜单类型
        int parentType = Constant.MenuType.CATALOG.getValue();
        if(menu.getParentId() != 0){
            parentType = sysMenuService.getById(menu.getParentId()).getType();
        }
        //判断目录和菜单的上级菜单类型是否正确
        if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
                menu.getType() == Constant.MenuType.MENU.getValue()){
            if(parentType != Constant.MenuType.CATALOG.getValue()){
                throw new RRException("上级菜单必须为目录类型");
            }
        }
        //判断按钮的上级菜单类型是否正确
        if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
            if(parentType != Constant.MenuType.MENU.getValue()){
                throw new RRException("上级菜单必须为菜单类型");
            }
        }

        if(menu.getType() == Constant.MenuType.MENU.getValue()){
            if(StringUtils.isBlank(menu.getUrl())){
                throw new RRException("url不能为空");
            }
        }


    }
}
